﻿<?php
/**
 * Oldy install script ;-(
 * @deprecated
 */

$request = request::instance( );
//function p($k){ return isset( $_POST[$k] ) ? $_POST[$k] : ''; }

$data = array( );
foreach( explode( ',', 'host,pages,public,title,author,dbhost,dbname,login,passwd' ) as $k )
{
	$pk = 'p_'.$k;
	$data[$k] = $request ->$pk;
}
if( count( array_filter($data) ) )
var_dump( $data );

$host = $_SERVER['SERVER_NAME'];
$clearhost = str_replace( array( 'beta.','.ru','.com','.org','.net' ), '', $host );

if(!$data['host']) $data['host'] = $host;
if(!$data['public']) $data['public'] = 'index,auth';
if(!$data['title']) $data['title'] = $clearhost;
if(!$data['dbhost']) $data['dbhost'] = 'localhost';
if(!$data['dbname']) $data['dbname'] = join( '_', array_reverse( explode( '.', $host ) ) );

$title = "Enter primal requisites";
$page = <<<page
<p>need settings. you can auto generate one from distribute default</p>
<form method="post">
<fieldset>
<legend>Description</legend>
<label for="host">host:</label><input id="host" name="host" type="text" value="{host}" /><br/>
<label for="pages">pages:</label><select id="pages" name="pages"><option value="base">DB</option><option value="public">Controllers</option><option value="both" selected="true">Both</option></select><br/>
<label for="public">public:</label><input id="public" name="public" type="text" value="{public}" /> (index,auth,...)<br/>
<label for="title">title:</label><input id="title" name="title" type="text" value="{title}" /><br/>
<label for="author">author:</label><input id="author" name="author" type="text" value="{author}" /><br/>
</fieldset>
<fieldset>
<legend>Database</legend>
<label for="dbhost">dbhost:</label><input id="dbhost" name="dbhost" type="text" value="{dbhost}" /><br/>
<label for="dbname">dbname:</label><input id="dbname" name="dbname" type="text" value="{dbname}" /><br/>
<label for="login">login:</label><input id="login" name="login" type="text" value="{login}" /><br/>
<label for="passwd">passwd:</label><input id="passwd" name="passwd" type="text" value="{passwd}" /><br/>
</fieldset>
<fieldset>
<label for="submit">&nbsp;</label><input id="submit" type="submit" value="Generate" /><br/>
</fieldset>
</form>
<p>Or create it manually.</p>
<hr/>
page;

foreach( $data as $k => $v )
{
	$page = str_replace( '{'.$k.'}', $v, $page );
}


if($data['dbhost']&&$data['login']&&$data['passwd'])
{
	$con = @mysql_connect( $data['dbhost'], $data['login'], $data['passwd'] );
	if( mysql_errno( ) ) $page .= '<p class="red">'.mysql_error( ).'</p>';
	else {
		mysql_query( 'set names utf8' );
		// select db
		mysql_select_db( $data['dbname'] );
		if( mysql_errno( ) ) $page .= '<p class="red">'.mysql_error( ).'</p>';
		else {
			//mysql_query(';');
			// all is good
			$sqls = explode( ';', file_get_contents( 'core/__install.sql' ) );
			foreach( $sqls as $sql )
			if( strlen( trim( $sql ) ) )
			{
			mysql_query( $sql );
			if( mysql_errno( ) ) $page .= '<p class="red">'.$sql.'<br/>'.mysql_error( ).'</p>';
			else $page .= '<p class="green">'.$sql.'</p>';
			}
			
			echo $cfg ->generate( $data );
		}
	}
}


//$cfg;

?>
<html>
<head><title>Установка Radiance Framework<?php if($title) print " — $title"; ?></title><style>label{width:80px;display:inline-block;}input[type=text]{width:300px;}.red{background:#a00;color:white;font-weight:bold;}.green{background:#0a0;color:white;font-weight:bold;}</style></head>
<body>
<h1><?=$title?></h1>
<?=$page?>
</body>
</html>
<?php die; ?>